export default {
	toast(content, duration = 1500) {
		if (content) {
			uni.showToast({
				icon: 'none',
				duration: duration, // 提示显示时间，单位为毫秒，默认为1500ms
				title: content
			})
		}
	},
	/**
	 * 格式化字符串
	 * @param {Object} str
	 */
	converStr(str) {
		if (typeof(str) == 'undefined' || str == null) {
			return '';
		}
		return str;
	},
	/**
	 * 格式化字符串
	 * @param {Object} str
	 */
	converJsonStr(str) {
		if (typeof(str) == 'undefined' || str == null || str == '') {
			return '{}';
		}
		return str;
	},
	/**转化对象
	 * @param {Object} obj
	 */
	converMap(obj) {
		console.log('obj对象' + JSON.stringify(obj));
		if (!obj) {
			return {};
		}
		return obj;
	},
	/**跳转页面
	 * @param {Object} path
	 */
	navigateTo(path) {
		uni.navigateTo({
			url: path,
			animationType: 'pop-in',
			animationDuration: 200
		})
	},
	/**跳转页面
	 * @param {Object} path
	 */
	redirectTo(path) {
		uni.redirectTo({
			url: path,
			animationType: 'pop-in',
			animationDuration: 200
		})
	},
	/**跳转页面
	 * @param {Object} path
	 */
	navigateTo(path, data) {
		uni.navigateTo({
			url: path,
			animationType: 'pop-in',
			animationDuration: 200,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage(data) {
					console.log(data)
				},
				someEvent(data) {
					console.log(data)
				}
			},
			success(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: data
				})
			}
		})
	},
	/**返回
	 * @param {Object} path
	 */
	navigateBack(path) {
		uni.navigateBack({
			delta: 1,
			animationType: 'pop-out',
			animationDuration: 200
		});
	},
	/**对象转url参数
	 * @param {Object} obj
	 */
	objectToUrl(obj) {
		let url = '';
		let i = 0;
		for (let key in obj) {
			if (i == 0) {
				url += ('?' + key + '=' + obj[key]);
			} else {
				url += ('&' + key + '=' + obj[key]);
			}
			i++;
		}
		return url;
	},
	/**字符串转数字
	 * @param {Object} str
	 */
	strToNumber(str) {
		if (!str) {
			return 0;
		}
		if (!uni.$u.test.number(str)) {
			return 0;
		}
		return parseInt(str);
	},
	/**秒转
	 * @param {Object} second
	 */
	secondFormat(second) {
		let time = {
			hour: 0,
			minute: 0,
			second: 0
		}
		let totalSecond = Math.floor(second);
		time.hour = Math.floor(totalSecond / 3600);
		totalSecond = totalSecond - (time.hour * 3600);
		time.minute = Math.floor(totalSecond / 60);
		time.second = totalSecond - (time.minute * 60)
		if (time.hour == 0) {
			return numberToStr(time.minute) + ':' + numberToStr(time.second)
		}
		return numberToStr(time.hour) + ':' + numberToStr(time.minute) + ':' + numberToStr(time.second)
	},
	/**视频时间转换
	 * @param {Object} time
	 */
	videoTimeFormat(time) {
		if (!time) {
			return 0;
		}
		let videoTime = time.split(':');
		if (videoTime.length == 2) {
			videoTime[0] = parseInt(videoTime[0])
			videoTime[1] = parseInt(videoTime[1])
			return (videoTime[0] * 60) + videoTime[1]
		} else if (videoTime.length == 3) {
			videoTime[0] = parseInt(videoTime[0])
			videoTime[1] = parseInt(videoTime[1])
			videoTime[2] = parseInt(videoTime[2])
			return videoTime[0] * 3600 + videoTime[1] * 60 + videoTime[2]
		}

	},
	/**是否为空
	 * @param {Object} str
	 */
	isEmpty(str) {
		if (uni.$u.test.digits(str) && str == 0) {
			return false
		}
		return uni.$u.test.isEmpty(str);
	},

}

function numberToStr(number) {
	if (number < 10) {
		return '0' + number;
	}
	return number + '';
}
